package com.resifind.www.dao.impl;

import com.resifind.www.constant.UserTypes;
import com.resifind.www.dao.OrderDao;
import com.resifind.www.dao.mapper.Mappers;
import com.resifind.www.po.Order;
import com.resifind.www.po.Room;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class OrderDaoImpl implements OrderDao {

    @Override
    public List<Order> queryOrderByUserId(int userId, String userType) {
        //联合查询订单表，客户表，房型表，商户表
        //language=MySQL
        StringBuilder sql = new StringBuilder("""
                select o.*, c.nickname as nickname, r.keyword as keyword, m.trade_name as trade_name, rn.room_number as room_number
                from resi_find.order o
                join resi_find.client c on o.client_id = c.id
                join resi_find.room r on o.room_id = r.id
                join resi_find.room_number rn on o.room_number_id = rn.id
                join resi_find.merchant m on r.merchant_id = m.id
                """);
        //根据用户类型查询订单信息
        if (userType.equals(UserTypes.CLIENT)) {
            sql.append("where o.client_id = ? and o.client_deleted = 0");
        } else if (userType.equals(UserTypes.MERCHANT)) {
            sql.append("where o.merchant_id = ? and o.merchant_deleted = 0");
        } else {
            return null;
        }
        //按日期降序排序
        sql.append(" order by o.date desc");
        Object[] params = {userId};
        return Mappers.orderMapper(sql.toString(), params);
    }

    @Override
    public Order queryOrderByOrderId(int orderId) {
        //language=MySQL
        String sql = """
                select o.*, c.nickname as nickname, r.keyword as keyword, m.trade_name as trade_name, rn.room_number as room_number
                from resi_find.order o
                join resi_find.client c on o.client_id = c.id
                join resi_find.room r on o.room_id = r.id
                join resi_find.room_number rn on o.room_number_id = rn.id
                join resi_find.merchant m on r.merchant_id = m.id
                where o.id = ?
                """;
        Object[] params = {orderId};
        List<Order> orders = Mappers.orderMapper(sql, params);
        return orders.isEmpty() ? null : orders.getFirst();
    }

    @Override
    public List<Order> queryOrderByRoomId(int roomId) {
        //language=MySQL
        String sql = """
                select *, null as nickname, null as keyword, null as trade_name, null as room_number
                from resi_find.order where room_id = ?
                """;
        Object[] params = {roomId};
        return Mappers.orderMapper(sql, params);
    }

    @Override
    public List<Order> queryOrderByRoomNumberId(int roomNumberId) {
        //language=MySQL
        String sql = """
                select *, null as nickname, null as keyword, null as trade_name, null as room_number
                from resi_find.order where room_number_id = ?
                """;
        Object[] params = {roomNumberId};
        return Mappers.orderMapper(sql, params);
    }

    @Override
    public boolean creatOrder(int clientId, Room room, int merchantId, int roomNumberId, Connection conn) {
        //language=MySQL
        String sql = "insert into resi_find.order (client_id, room_id, room_number_id, merchant_id, amount) values (?, ?, ?, ?, ?)";
        Object[] params = {clientId, room.getId(), roomNumberId, merchantId, room.getPrice()};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean deleteOrderByRoomId(int roomId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.order where room_id = ?";
        Object[] params = {roomId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean deleteOrderByRoomNumberId(int roomNumberId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.order where room_number_id = ?";
        Object[] params = {roomNumberId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean deleteOrderByOrderId(int orderId, Connection conn) {
        //language=MySQL
        String sql = "delete from resi_find.order where id = ?";
        Object[] params = {orderId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean fakeDeleteOrderByOrderId(int orderId, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.order set client_deleted = 1 where id = ?";
        Object[] params = {orderId};
        return JdbcUtils.update(sql, params, conn);
    }


    @Override
    public boolean updateOrderCheckStatus(int orderId, String newStatus, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.order set check_status = ? where id = ?";
        Object[] params = {newStatus, orderId};
        return JdbcUtils.update(sql, params, conn);
    }

    @Override
    public boolean updateOrderTradeStatus(int orderId, String newStatus, Connection conn) {
        //language=MySQL
        String sql = "update resi_find.order set trade_status = ? where id = ?";
        Object[] params = {newStatus, orderId};
        return JdbcUtils.update(sql, params, conn);
    }
}
